import { createStore } from 'vuex'

export default createStore({
  state: {
    list:[],
    copyList:[],//复制一份
  },
  getters: {
    getPrice(state){
        // console.log(state.list);
        let sub = 0
        let add = 0
        state.list.forEach(item=>{
                sub+=Number(item.sub)//支出
                add+=Number(item.add)//收入
            
        })
        return {sub,add}
    }
  },
  mutations: {
    add(state,obj){
        // console.log(obj.type);
        // state.list.push(obj)
        // 判断是否是同一天
        let i = state.list.findIndex(item=>item.time == obj.date)
        // 找到同一天的
        if(i!=-1){
            
            state.list[i].type = obj.type
            console.log(obj.type);
            state.list[i].children.push(obj)
            let sub = 0
            let add = 0
            state.list[i].children.forEach(item=>{
               
                if(item.type == 'pay'){
                    sub+=Number(item.value)
                }else{
                    add+=Number(item.value)
                }
            })
            state.list[i].sub = sub
            state.list[i].add = add

        }else{

            // 不是同一天的
            if(obj.type =='pay'){
                state.list.push({
                    title:obj.title,//做筛选用
                    sub:obj.value,//支出，
                    add:0,//收入
                    type:obj.type,
                    children:[obj],
                    time:obj.date
                })

            }else{
                state.list.push({
                    type:obj.type,
                    title:obj.title,
                    sub:0,//支出，
                    add:obj.value,//收入
                    children:[obj],
                    time:obj.date
                })
            }
            
        }
        // console.log(state.list)
        state.copyList = [...state.list]

    },
    filterClick(state,title){
        console.log(state.list);
        // 如果是全部类型
        if(title == '全部类型'){
            state.list = state.copyList
            return
        }
        
        state.list = state.copyList.map(item=>{
            
            let arr = item.children.filter(it=>it.title ==title)
            item.children = arr
            return item
        })
        console.log(state.list);
    //   state.list =  state.copyList.map(item=>{
    //        let arr =  item.children.filter(it=>it.title == title)
    //        return arr
           
    //     })
       
       

    }
  },
  actions: {
  },
  modules: {
  }
})
